import { EggAppConfig, EggAppInfo, PowerPartial } from "egg";
import * as dotenv from "dotenv";
dotenv.config();

export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<EggAppConfig>;

  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + "_1652521338106_9568";

  // add your egg config in here
  config.middleware = [];

  // add your special config in here
  const bizConfig = {
    sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
  };

  // crsf安全请求保护
  config.security = {
    csrf: {
      enable: false,
    },
    domainWhiteList: ["http://localhost:8080"],
  };

  // 参数校验
  config.validate = {
    // 配置参数校验器，基于parameter
    // 对参数可以使用 convertType 规则进行类型转换
    convert: true,
    // 限制被验证值必须是一个对象。
    // validateRoot: false,
    // 强制把"'', NaN, Null 等转为undefined，注意，开启此属性，自定义rule时不能检测 ”必填“，回走插件的默认校验
    widelyUndefined: false,
  };

  // bcrypt密码加配置
  config.bcrypt = {
    saltRounds: 10,
  };

  // jwt插件配置
  config.jwt = {
    enable: true,
    secret: process.env.JWT_SECRET || "",
    ignore: [
      "/api/orgRegister",
      "/api/sendVeriCode",
      "/api/verifyPhoneCode",
      "/api/loginByCode",
      "/api/loginByPass",
    ],
  };

  // redis插件配置
  config.redis = {
    client: {
      port: parseInt(process.env.REDIS_PORT as string),
      host: `${process.env.SERVER_BASE_TWO_IP}`,
      password: `${process.env.REDIS_PWD}`,
      db: 0,
    },
  };

  // egg-mongoose插件配置
  config.mongoose = {
    url: `mongodb://${process.env.SERVER_BASE_TWO_IP}:${process.env.MONGO_INITDB_ROOT_PORT}/${process.env.MONGO_INIT_DB}`,
    options: {
      user: process.env.MONGO_INITDB_ROOT_USERNAME,
      pass: process.env.MONGO_INITDB_ROOT_PASSWORD,
      // 告诉数据库我使用的用户是最高权限的root用户，这就可以使用root用户登陆任意数据库了
      authSource: "admin",
    },
  };

  // the return config will combines to EggAppConfig
  return {
    ...config,
    ...bizConfig,
  };
};
